package com.kt.service.impl;

import com.kt.client.ProductClient;
import com.kt.dataobject.OrderDetail;
import com.kt.dataobject.OrderMaster;
import com.kt.dataobject.ProductInfo;
import com.kt.dto.CartDTO;
import com.kt.dto.OrderDTO;
import com.kt.enums.OrderStatus;
import com.kt.enums.PayStatusEnum;
import com.kt.repository.OrderDetailRepository;
import com.kt.repository.OrderMasterRepository;
import com.kt.service.OrderService;
import com.kt.utils.Keyutil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDetailRepository orderDetailRepository;
    @Autowired
    private OrderMasterRepository orderMasterRepository;
    @Autowired
    private ProductClient productClient;
    @Override
    public OrderDTO create(OrderDTO orderDTO) {
        String orderId=Keyutil.genUniqueKey();

        //1.查询商品信息(调用商品服务)
        List<String> productIdList = orderDTO.getOrderDetailList().stream()
                .map(OrderDetail::getProductId)
                .collect(Collectors.toList());
        List<ProductInfo> ProductInfolist=productClient.listForOrder(productIdList);

        //2.计算总价
        BigDecimal orderAmout=new BigDecimal(BigInteger.ZERO);

        for(OrderDetail orderDetail:orderDTO.getOrderDetailList()){
            for(ProductInfo productInfo:ProductInfolist){
            if(productInfo.getProductId().equals(orderDetail.getProductId())){
                //单价*数量
                orderAmout=productInfo.getProductPrice()
                        .multiply(new BigDecimal(orderDetail.getProductQuantity()))
                        .add(orderAmout);
                        //订单详情 赋值
                        //BeanUtils 的这个复制方法会把 Null也复制过去
                BeanUtils.copyProperties(productInfo,orderDetail);
                //所以这个orderDetail类的 id属性需要重新复制
                orderDetail.setDetailId(orderId);
                orderDetail.setOrderId(Keyutil.genUniqueKey());
                    //订单详情入库
                orderDetailRepository.save(orderDetail);
                  }
            }
        }
        //3.扣库存
        List<CartDTO> cartDTOList=orderDTO.getOrderDetailList().stream()
                .map(e -> new CartDTO(e.getProductId(),e.getProductQuantity()))
                .collect(Collectors.toList());
        productClient.decreaseStock(cartDTOList);



        //4.订单入库
        OrderMaster orderMaster=new OrderMaster();
        orderDTO.setOrderId(orderId);
        BeanUtils.copyProperties(orderDTO,orderMaster);
        orderMaster.setOrderAmount(orderAmout);
        orderMaster.setOrderStatus(OrderStatus.NEW.getCode());
        orderMaster.setPayStatus(PayStatusEnum.WAIT.getCode());
        orderMasterRepository.save(orderMaster);

        return orderDTO;
    }
}
